package gui;

import flexibility.sendMail;
import guiCode.checkDataBase;
import guiCode.loginScreenCode;

import java.awt.*;
import javax.swing.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.UnknownHostException;

public class loginScreen {
	private static JTextField textField;
	private static JPasswordField passwordField;
	private static JFrame loginScreen;
	private static JLabel lblUsername;
	private static JLabel lblPassword;
	private static JLabel lbUserNameErr;
	private static JLabel lbPasswordErr;;
	static loginScreenCode lsc = new loginScreenCode();
	private static JButton btnResetMyAccess;
	private checkDataBase busy = new checkDataBase();
	private static JTextField txtServerIP;
	private static JTextField textServerPort;
	private static JLabel lblServerIsUnavaliable;
	public static void main(String[] args) {
		createWindow();
		lsc.fillServerDetails();
		/*
		 * loginScreen.addWindowListener(new WindowAdapter() { public void
		 * windowClosing(WindowEvent e) { lsc.signOut(); } });
		 */
	}

	public void closeLoginScreen() {
		/*
		 * closeLognScreen handles the closing of the login window
		 */
		while (busy.getBusy() == true) {
			
		}
		loginScreen.dispose();

	}

	private static void createWindow() {
		/*
		 * createWindow creates the login window
		 */

		loginScreen = new JFrame("Login to Academic Information System");
		loginScreen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		loginScreen.setSize(470, 367);
		loginScreen.setResizable(false);
		loginScreen.move(450, 250);
		SpringLayout springLayout = new SpringLayout();
		loginScreen.getContentPane().setLayout(springLayout);

		lblUsername = new JLabel("Username");
		springLayout.putConstraint(SpringLayout.NORTH, lblUsername, 17, SpringLayout.NORTH, loginScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, lblUsername, 67, SpringLayout.WEST, loginScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, lblUsername, -303, SpringLayout.SOUTH, loginScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, lblUsername, -229, SpringLayout.EAST, loginScreen.getContentPane());
		lblUsername.setFont(new Font("Tahoma", Font.BOLD, 12));
		loginScreen.getContentPane().add(lblUsername);

		textField = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, textField, 6, SpringLayout.SOUTH, lblUsername);
		springLayout.putConstraint(SpringLayout.WEST, textField, 67, SpringLayout.WEST, loginScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, textField, -266, SpringLayout.SOUTH, loginScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, textField, -214, SpringLayout.EAST, loginScreen.getContentPane());
		loginScreen.getContentPane().add(textField);
		textField.setColumns(10);

		lblPassword = new JLabel("Password");
		springLayout.putConstraint(SpringLayout.WEST, lblPassword, 67, SpringLayout.WEST, loginScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, lblPassword, 0, SpringLayout.EAST, lblUsername);
		lblPassword.setFont(new Font("Tahoma", Font.BOLD, 12));
		loginScreen.getContentPane().add(lblPassword);

		passwordField = new JPasswordField();
		springLayout.putConstraint(SpringLayout.NORTH, passwordField, 6, SpringLayout.SOUTH, lblPassword);
		springLayout.putConstraint(SpringLayout.WEST, passwordField, 67, SpringLayout.WEST, loginScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, passwordField, -188, SpringLayout.SOUTH, loginScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, passwordField, 0, SpringLayout.EAST, textField);
		loginScreen.getContentPane().add(passwordField);

		lbUserNameErr = new JLabel("Enter your username.");
		springLayout.putConstraint(SpringLayout.NORTH, lblPassword, 6, SpringLayout.SOUTH, lbUserNameErr);
		springLayout.putConstraint(SpringLayout.NORTH, lbUserNameErr, 6, SpringLayout.SOUTH, textField);
		springLayout.putConstraint(SpringLayout.WEST, lbUserNameErr, 0, SpringLayout.WEST, lblUsername);
		springLayout.putConstraint(SpringLayout.EAST, lbUserNameErr, -229, SpringLayout.EAST, loginScreen.getContentPane());
		lbUserNameErr.setForeground(Color.RED);
		loginScreen.getContentPane().add(lbUserNameErr);

		lbPasswordErr = new JLabel("Enter your password.");
		springLayout.putConstraint(SpringLayout.NORTH, lbPasswordErr, 6, SpringLayout.SOUTH, passwordField);
		springLayout.putConstraint(SpringLayout.WEST, lbPasswordErr, 0, SpringLayout.WEST, lblUsername);
		springLayout.putConstraint(SpringLayout.EAST, lbPasswordErr, -229, SpringLayout.EAST, loginScreen.getContentPane());
		JButton btnSignIn = new JButton("Sign In");
		springLayout.putConstraint(SpringLayout.WEST, btnSignIn, 67, SpringLayout.WEST, loginScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, btnSignIn, -289, SpringLayout.EAST, loginScreen.getContentPane());
		lbPasswordErr.setForeground(Color.RED);
		loginScreen.getContentPane().add(lbPasswordErr);

		btnSignIn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				actionListener();
			}
		});

		textField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				actionListener();
			}
		});

		passwordField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				actionListener();
			}
		});

		btnSignIn.setBackground(SystemColor.control);
		btnSignIn.setForeground(SystemColor.desktop);
		btnSignIn.setBorder(BorderFactory.createLineBorder(Color.BLUE));

		loginScreen.getContentPane().add(btnSignIn);

		JButton btnForgotYourPassword = new JButton("Forgot your password?");
		springLayout.putConstraint(SpringLayout.SOUTH, btnSignIn, -6, SpringLayout.NORTH, btnForgotYourPassword);
		springLayout.putConstraint(SpringLayout.EAST, btnForgotYourPassword, 0, SpringLayout.EAST, textField);
		springLayout.putConstraint(SpringLayout.NORTH, btnForgotYourPassword, 294, SpringLayout.NORTH, loginScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, btnForgotYourPassword, 0, SpringLayout.WEST, lblUsername);
		springLayout.putConstraint(SpringLayout.SOUTH, btnForgotYourPassword, -30, SpringLayout.SOUTH, loginScreen.getContentPane());
		btnForgotYourPassword.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (textField.getText().isEmpty() == true) {
					lbUserNameErr.setVisible(true);
					textField.setBorder(BorderFactory
							.createLineBorder(Color.red));
					lbPasswordErr.setVisible(false);
					passwordField.setBorder(BorderFactory
							.createLineBorder(Color.black));
				} else {
					
					textField.setBorder(BorderFactory
							.createLineBorder(Color.black));
					lsc.setUserName(textField.getText());
					
					lbUserNameErr.setVisible(false);
					setConnection();
					lsc.recoverType(2);
					
				}

			}

		});
		
		btnForgotYourPassword.setBackground(SystemColor.control);
		loginScreen.getContentPane().add(btnForgotYourPassword);

		btnResetMyAccess = new JButton("Reset my access");
		springLayout.putConstraint(SpringLayout.NORTH, btnResetMyAccess, 6, SpringLayout.SOUTH, btnForgotYourPassword);
		springLayout.putConstraint(SpringLayout.WEST, btnResetMyAccess, 67, SpringLayout.WEST, loginScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, btnResetMyAccess, -10, SpringLayout.SOUTH, loginScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, btnResetMyAccess, 0, SpringLayout.EAST, textField);
		btnResetMyAccess.setBackground(SystemColor.control);
		btnResetMyAccess.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (textField.getText().isEmpty() == true) {
					lbUserNameErr.setVisible(true);
					textField.setBorder(BorderFactory
							.createLineBorder(Color.red));
					lbPasswordErr.setVisible(false);
					passwordField.setBorder(BorderFactory
							.createLineBorder(Color.black));
				} else {
					lbUserNameErr.setVisible(false);
					textField.setBorder(BorderFactory
							.createLineBorder(Color.black));
					lsc.setUserName(textField.getText());
					
					lsc.recoverType(1);
				}

			}
		});
		loginScreen.getContentPane().add(btnResetMyAccess);
		
		txtServerIP = new JTextField();
		springLayout.putConstraint(SpringLayout.EAST, txtServerIP, -272, SpringLayout.EAST, loginScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, txtServerIP, 0, SpringLayout.WEST, lblUsername);
		springLayout.putConstraint(SpringLayout.SOUTH, txtServerIP, -110, SpringLayout.SOUTH, loginScreen.getContentPane());
		loginScreen.getContentPane().add(txtServerIP);
		txtServerIP.setColumns(10);
		
		JLabel lblServerIpAddress = new JLabel("Server IP Address and Port ");
		springLayout.putConstraint(SpringLayout.NORTH, txtServerIP, 6, SpringLayout.SOUTH, lblServerIpAddress);
		springLayout.putConstraint(SpringLayout.NORTH, lblServerIpAddress, 6, SpringLayout.SOUTH, lbPasswordErr);
		springLayout.putConstraint(SpringLayout.WEST, lblServerIpAddress, 0, SpringLayout.WEST, lblUsername);
		springLayout.putConstraint(SpringLayout.EAST, lblServerIpAddress, -214, SpringLayout.EAST, loginScreen.getContentPane());
		lblServerIpAddress.setFont(new Font("Tahoma", Font.BOLD, 12));
		loginScreen.getContentPane().add(lblServerIpAddress);
		
		lblServerIsUnavaliable = new JLabel("Server is unavaliable");
		springLayout.putConstraint(SpringLayout.NORTH, btnSignIn, 8, SpringLayout.SOUTH, lblServerIsUnavaliable);
		springLayout.putConstraint(SpringLayout.NORTH, lblServerIsUnavaliable, 6, SpringLayout.SOUTH, txtServerIP);
		lblServerIsUnavaliable.setForeground(new Color(255, 0, 0));
		springLayout.putConstraint(SpringLayout.WEST, lblServerIsUnavaliable, 67, SpringLayout.WEST, loginScreen.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, lblServerIsUnavaliable, 0, SpringLayout.EAST, textField);
		lblServerIsUnavaliable.setVisible(false);
		loginScreen.getContentPane().add(lblServerIsUnavaliable);
		
		textServerPort = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, textServerPort, 0, SpringLayout.NORTH, txtServerIP);
		springLayout.putConstraint(SpringLayout.WEST, textServerPort, 3, SpringLayout.EAST, txtServerIP);
		springLayout.putConstraint(SpringLayout.SOUTH, textServerPort, 0, SpringLayout.SOUTH, txtServerIP);
		springLayout.putConstraint(SpringLayout.EAST, textServerPort, 61, SpringLayout.EAST, txtServerIP);
		loginScreen.getContentPane().add(textServerPort);
		textServerPort.setColumns(10);
		lbUserNameErr.setVisible(false);
		lbPasswordErr.setVisible(false);
		loginScreen.setVisible(true);

	}

	public void showMessages(String msg, String title, int type) {
		if (type == 1) {
			JOptionPane.showMessageDialog(loginScreen, msg, title,
					JOptionPane.ERROR_MESSAGE);
		}
		if (type == 2) {
			JOptionPane.showMessageDialog(loginScreen, msg, title,
					JOptionPane.INFORMATION_MESSAGE);
		}
	}

	public void setServerDetails(String ip, Integer port){
		//this will set the server ip address
		txtServerIP.setText(ip);
		textServerPort.setText(port.toString());
	}
	
	private static void actionListener() {
		if (textField.getText().isEmpty() == true) {
			lbUserNameErr.setVisible(true);
			textField.setBorder(BorderFactory.createLineBorder(Color.red));
		} else {
			lbUserNameErr.setVisible(false);
			textField.setBorder(BorderFactory.createLineBorder(Color.black));
		}
		if (passwordField.getText().isEmpty() == true) {
			lbPasswordErr.setVisible(true);
			passwordField.setBorder(BorderFactory.createLineBorder(Color.red));
		} else {
			lbPasswordErr.setVisible(false);
			passwordField
					.setBorder(BorderFactory.createLineBorder(Color.black));
		}
		if (textField.getText().isEmpty() == false
				&& passwordField.getText().isEmpty() == false) {
			lsc.setUserName(textField.getText());
			lsc.setPassword(passwordField.getText());	
			setConnection();
			lsc.setInfo();
		}
	}
	

	
	public static void setConnection(){
		if(lsc.connectToServer(txtServerIP.getText(),textServerPort.getText())){
			lblServerIsUnavaliable.setVisible(false);
		}
		else{
			lblServerIsUnavaliable.setVisible(true);
			
		}
	}
}
